﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using Styx.Common;

namespace rndbuddy
{
    public partial class gui_settings : Form
    {
        private static bool debug_toggled = false;

        public gui_settings()
        {
            InitializeComponent();
        }

        private void gui_settings_Load(object sender, EventArgs e)
        {
            settings.parseConfig();

            //check for key valid
            if (settings.valid_key == true)
            {
                this.button1.Enabled = false;
                this.button2.Enabled = true;
            }
            else
            {
                this.button1.Enabled = true;
                this.button2.Enabled = false;
            }

            lbl_version.Text = settings.version.ToString();

            //load images to gui
            pictureBox1.Image = Image.FromFile(Path.Combine(settings.app_data_dir, "rndbuddy_a.png"));
            pictureBox2.Image = Image.FromFile(Path.Combine(settings.app_data_dir, "donate.gif"));

            //resize to hide debug buttons
            this.Width = 290;

            //if rndbuddy was not initialized the timer wont start (issue: RNDB-1)
            timer1.Enabled = rndbuddy._init;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            form_signup f = new form_signup();
            f.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ProfileHandler.get_profile();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                lbl_profile_timer.Text = (rndbuddy.t_profile.Interval/1000).ToString();
                lbl_ph_tot_refresh.Text = ProfileHandler.total_refresh.ToString();
                lbl_moved.Text = Math.Round(movement_watcher.moved,1).ToString();
                lbl_watcher_ticks.Text = movement_watcher.ticked.ToString();
                lbl_distance_to_end.Text = Math.Round(ProfileHandler.distance_to_end,1).ToString();
                lbl_current_profile.Text = ProfileHandler.current_profile.ToString();
                lbl_cs_nodes.Text = crowdsourcer.amount_of_nodes().ToString();
                lbl_profile_zone.Text = ProfileHandler.profile_zone.ToString();
                lbl_pb_des_zo.Text = ProfileHandler.pb_desired_zone;
                lbl_profile_duration.Text = Math.Floor(System.DateTime.Now.Subtract(ProfileHandler.profile_start).TotalSeconds).ToString();
                
                //grace display
                lbl_unmount_lock.Text = Math.Round((movement_controller.no_unmount_until-System.DateTime.Now).TotalSeconds,0).ToString() + " sec";

                //some fancy colours
                if (System.DateTime.Now < movement_controller.no_unmount_until)
                    lbl_unmount_lock.ForeColor = Color.Red;
                else
                    lbl_unmount_lock.ForeColor = Color.Green;

                if (DateTime.Now.AddDays(350) < settings.timed_kill)
                    lbl_timed_kill.Text = "never";
                else
                    lbl_timed_kill.Text = Math.Round((settings.timed_kill - System.DateTime.Now).TotalSeconds, 0).ToString() + " sec.";

            }
            catch (Exception ex)
            {
                rndbuddy.logcrit("Exception in timer1_tick at gui_settings");
                rndbuddy.logcrit(ex.Message);
                rndbuddy.logcrit(ex.StackTrace);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Toon t = new Toon();
            t.poll();
            rndbuddy.log("NAME = " + t.Name);
            rndbuddy.log("CLASS = " + t.Class);
            rndbuddy.log("LEVEL = " + t.Level);
            rndbuddy.log("REALM = " + t.Realm);
            rndbuddy.log("MAP = " + t.MapID+t.MapName);
            rndbuddy.log("ZONE = " + t.ZoneID+t.ZoneText);
            rndbuddy.log("FACTION = " + t.FactionID);
            rndbuddy.log("BAGS_FREE = " + t.BagsFree);
            rndbuddy.log("MONEY = " + t.Money);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            ProfileHandler.calc_and_log_nph();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            rndbuddy.log(crowdsourcer.merge_and_report(false));
        }

        private void button7_Click(object sender, EventArgs e)
        {
            server_pipe.report_crowdsourcer_data(false);
        }

        private void btn_remote_settings_Click(object sender, EventArgs e)
        {
            settings.parse_remote_settings();
        }

        private void btn_players_around_Click(object sender, EventArgs e)
        {
            PlayerSecurity.who_is_targeting_me(true);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            rndbuddy.forceQuit(true,"Test kill issued by user");
        }

        private void button9_Click(object sender, EventArgs e)
        {
            rndbuddy.take_screenshot();
        }

        private void button10_Click(object sender, EventArgs e)
        {
            rndbuddy.log("wowpath is " + settings.wowpath);
            string scrpath = settings.wowpath + @"\Screenshots\";
            rndbuddy.log("screenpath is " + scrpath);
        }

        private void button11_Click(object sender, EventArgs e)
        {
            server_pipe.notify("This is a example notification Message!");
        }

        private void button12_Click(object sender, EventArgs e)
        {
            movement_controller.jump();
        }

        private void button13_Click(object sender, EventArgs e)
        {
            PlayerSecurity.dump_followers();
        }

        private void button14_Click(object sender, EventArgs e)
        {
            PlayerSecurity.PlayerFollowDetection();
        }

        private void button15_Click(object sender, EventArgs e)
        {
            settings.timed_kill = DateTime.Now.AddDays(360);
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            Process.Start("https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=D44S6E2WW4X5Y");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            switch (debug_toggled)
            {
                case false: this.Width = 530; debug_toggled = true; break;
                case true: this.Width = 290; debug_toggled = false; break;
            }
        }

        private void button16_Click(object sender, EventArgs e)
        {
            rndbuddy.log(Utilities.AssemblyDirectory+@"\Bots\Professionbuddy\Profiles\AHBot.xml");
        }

        private void button18_Click(object sender, EventArgs e)
        {
            ProfileHandler.BuildPBCoreProfile();
            ProfileHandler.load_professionbuddy_farming_profile();
        }

        private void button17_Click_1(object sender, EventArgs e)
        {
            ProfileHandler.LoadBotBase("Gatherbuddy2");
            ProfileHandler.get_profile();
            ProfileHandler.TreeAction("start");
        }

        private void button18_Click_1(object sender, EventArgs e)
        {
            ProfileHandler.LoadBotBase("ProfessionBuddy");
            ProfileHandler.get_profile();
            ProfileHandler.TreeAction("start");
        }

        private void button19_Click(object sender, EventArgs e)
        {
            ProfileHandler.load_professionbuddy_ahbot();
        }
        
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Process.Start("https://rndbuddy.com/");
        }

        private void button20_Click(object sender, EventArgs e)
        {
            rndbuddy.log(ProfileHandler.current_hb_profile_name());
        }

        private void button21_Click(object sender, EventArgs e)
        {
            suggest_blackspot f = new suggest_blackspot();
            f.Show();
        }

        private void button22_Click(object sender, EventArgs e)
        {
            movement_controller.unstuck_attempt();
        }

        private void button22_Click_1(object sender, EventArgs e)
        {
            rndbuddy.log(settings.app_data_dir);
            rndbuddy.log(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
        }

        private void lbl_last_moved_Click(object sender, EventArgs e)
        {

        }

        private void button23_Click(object sender, EventArgs e)
        {
            //rndbuddy.log(BotManager.Instance.Bots[2].Name);
        }

    }
}
